package com.dao.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sql.MysqlSql;
import com.sql.OracleSql;
import com.util.StaticEunm;

public class TransDao {
	public List<Map<String,String>> getMenuData(String owner,String type,String url,String username ,String password) {
		Connection conn =null;
		PreparedStatement ps=null;
		ResultSet rs =null;
		List<Map<String,String>> list =null;
		try {
			
			String get_tab_comments =null;
			if(type.equals(StaticEunm.DATABASE_ORACLE)){
				Class.forName(StaticEunm.DIVER_NAME_ORACLE);
				get_tab_comments=OracleSql.get_tab_comments;
			}else {
				Class.forName(StaticEunm.DIVER_NAME_MYSQL);
				get_tab_comments=MysqlSql.get_tab_comments;
			}
			conn = DriverManager.getConnection(url, username, password);
			System.out.println("******************* get_tab_comments SQL ****************************");
			System.out.println(get_tab_comments);
			System.out.println("******************* owner DATA ****************************");
			System.out.println(owner);
			ps = conn.prepareStatement(get_tab_comments);
			ps.setString(1, owner.toUpperCase());
			rs = ps.executeQuery();
			list =new ArrayList<Map<String,String>>();
			int conut=1;
			while (rs.next()) {
				Map<String,String> map= new HashMap<String, String>();
				map.put("NUM", String.valueOf(conut));
				map.put("TABLE_NAME", rs.getString("TABLE_NAME"));
				map.put("COMMENTS", rs.getString("COMMENTS"));
				map.put("TABLE_TYPE", rs.getString("TABLE_TYPE"));
				conut++;
				list.add(map);
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				if(rs!=null) {
					rs.close();
				}
				if(ps!=null) {
					ps.close();
				}
				if(conn!=null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}
	
	public List<Map<String,String>> getTableDate(String owner,String type,String url,String username ,String password,String tablename) {
		Connection conn =null;
		PreparedStatement ps=null;
		ResultSet rs =null;
		List<Map<String,String>> list =null;
		try {
			String get_tab_col =null;
			if(type.equals(StaticEunm.DATABASE_ORACLE)){
				Class.forName(StaticEunm.DIVER_NAME_ORACLE);
				get_tab_col=OracleSql.get_tab_col.toString();
			}else {
				Class.forName(StaticEunm.DIVER_NAME_MYSQL);
				get_tab_col=MysqlSql.get_tab_col.toString();
			}
			conn = DriverManager.getConnection(url, username, password);
			System.out.println("******************* get_tab_col SQL ****************************");
			System.out.println(get_tab_col);
			System.out.println("******************* get_tab_col DATA ****************************");
			System.out.println(owner);
			ps = conn.prepareStatement(get_tab_col);
			ps.setString(1, tablename);
			if(type.equals(StaticEunm.DATABASE_MYSQL)){
				ps.setString(2, owner.toUpperCase());
			}
			rs = ps.executeQuery();
			list =new ArrayList<Map<String,String>>();
			while (rs.next()) {
				Map<String,String> map= new HashMap<String, String>();
				map.put("TABLE_NAME", rs.getString("TABLE_NAME"));
				map.put("COLUMN_ID", rs.getString("COLUMN_ID"));
				map.put("COLUMN_NAME", rs.getString("COLUMN_NAME"));
				map.put("COMMENTS", rs.getString("COMMENTS"));
				map.put("DATA_TYPE", rs.getString("DATA_TYPE"));
				map.put("DATA_LENGTH", rs.getString("DATA_LENGTH"));
				map.put("DATA_SCALE", rs.getString("DATA_SCALE"));
				map.put("NULLABLE", rs.getString("NULLABLE"));
				map.put("DATA_DEFAULT", rs.getString("DATA_DEFAULT"));
				map.put("PrimaryKey", rs.getString("PrimaryKey"));
				list.add(map);
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				if(rs!=null) {
					rs.close();
				}
				if(ps!=null) {
					ps.close();
				}
				if(conn!=null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}
}
